package com.peakmain.wanandroid.loginmodule;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.peakmain.wanandroid.loginmodule.base.BaseLoginActivity;
import com.peakmain.wanandroid.loginmodule.constant.LoginConstant;
import com.peakmain.wanandroid.loginmodule.contract.LoginContract;
import com.peakmain.wanandroid.loginmodule.presenter.LoginPresenter;

import butterknife.BindView;
import butterknife.OnClick;
@Route(path = "/activity/loginActivity")
public class LoginActivity extends BaseLoginActivity<LoginPresenter> implements LoginContract.View {

    @BindView(R2.id.tv_al_account)
    EditText tvAccount;
    @BindView(R2.id.tv_al_password)
    EditText tvPassword;

    @BindView(R2.id.toolbar)
    Toolbar mToolBar;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initInjector() {
        ARouter.getInstance().inject(this);

        mActivityComponent.inject(this);
    }

    @Override
    protected void initView() {
        TextView textView = (TextView) mToolBar.getChildAt(0);
        textView.getLayoutParams().width = LinearLayout.LayoutParams.MATCH_PARENT;//填充父类
        textView.setGravity(Gravity.CENTER_HORIZONTAL);//水平居中，CENTER，即水平也垂直，自选
        mToolBar.setTitle("登录");
    }

    @Override
    public void showLoginSuccess() {

        showSuccess("登录成功!");
        SPUtils.getInstance().put(LoginConstant.ACCOUNT, tvAccount.getText().toString().trim());
        SPUtils.getInstance().put(LoginConstant.PASSWORD, tvPassword.getText().toString().trim());
        ARouter.getInstance().build("/activity/MainActivity")
                .navigation();
        finish();
    }

    @OnClick({R2.id.regitster, R2.id.login})
    public void onClick(View view) {
        if (view.getId() == R.id.regitster) {
            Intent intent = new Intent(this, RegisterActivity.class);
            startActivity(intent);
        } else if (view.getId() == R.id.login) {
            String account = tvAccount.getText().toString();
            String password = tvPassword.getText().toString();
            if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(password)) {

                mPresenter.Login(account, password);
            } else {
                ToastUtils.showShort("输入框不能为空");
            }

        }

    }

}
